/********************************************************************************
 * Project Name		[JavaEE_Components]
 * File Name     	[SearchingMails.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package javamail.mail;

import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.URLName;
import javax.mail.search.AndTerm;
import javax.mail.search.BodyTerm;
import javax.mail.search.ComparisonTerm;
import javax.mail.search.FromStringTerm;
import javax.mail.search.IntegerComparisonTerm;
import javax.mail.search.NotTerm;
import javax.mail.search.OrTerm;
import javax.mail.search.SearchTerm;
import javax.mail.search.SentDateTerm;
import javax.mail.search.SizeTerm;
import javax.mail.search.SubjectTerm;

import org.junit.Before;
import org.junit.Test;

import javamail.utils.MailConstants;
import javamail.utils.PasswordUtils;

/**
 * 搜索邮件
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class SearchingMails {

	private static Properties properties = null;

	private static String emailAddress = "javamail_sns@sina.com";
	private static URLName url;

	@Before
	public void init() {
		properties = new Properties();
		properties.put("mail.pop3.auth", MailConstants.IS_AUTHORIZATION_NEED);
		properties.put("mail.debug", String.valueOf(MailConstants.IS_DEBUG_MOD_ENABLED));
		url = new URLName(MailConstants.PROTOCAL_POP3, MailConstants.POP3_HOST_OF_SINA,
				Integer.parseInt(MailConstants.POP3_PORT_OF_SINA), null, emailAddress,
				PasswordUtils.readPassword(emailAddress));
	}

	/**
	 * 搜索邮件
	 * 
	 * @throws Exception
	 */
	public static void searchMails() throws Exception {
		// 创建Session对象并连接到指定的邮箱
		Session session = Session.getInstance(properties);
		Store store = session.getStore(url);
		store.connect(emailAddress, PasswordUtils.readPassword(emailAddress));

		// 获得收件箱
		Folder folder = store.getFolder("INBOX");
		// 以只读权限打开收件箱, 还可以配置为Folder.READ_WRITE
		folder.open(Folder.READ_ONLY);

		// 得到收件箱里所以的邮件
		Message[] messages = searchMails(folder);
		System.out.println("一共有邮件" + messages.length + "封");
	}

	/**
	 * 根据条件搜索邮件
	 * 
	 * @param folder
	 * @return
	 * @throws MessagingException
	 */
	@SuppressWarnings("unused")
	private static Message[] searchMails(Folder folder) throws MessagingException {
		// 搜索主题
		String subject = "java";
		SearchTerm subjectTerm = new SubjectTerm(subject);

		// 搜索发件人
		String emailFrom = "18505106250@163.com";
		SearchTerm fromTerm = new FromStringTerm(emailFrom);

		// 搜索内容
		String content = "html";
		SearchTerm bodyTerm = new BodyTerm(content);

		SearchTerm andTerm = new AndTerm(subjectTerm, fromTerm);
		SearchTerm notTerm = new NotTerm(bodyTerm);
		SearchTerm orTerm = new OrTerm(andTerm, notTerm);

		// 搜索周一到今天所有的邮件
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		Date monday = calendar.getTime();
		SearchTerm geTerm = new SentDateTerm(ComparisonTerm.GE, monday);
		SearchTerm leTerm = new SentDateTerm(ComparisonTerm.LE, new Date());
		SearchTerm dateRangeTerm = new AndTerm(geTerm, leTerm);

		// 邮件大小
		int size = 1024 * 100; // 100kb
		SearchTerm mailSizeTerm = new SizeTerm(IntegerComparisonTerm.GE, size);

		return folder.search(dateRangeTerm);
	}

	@Test
	public void testSearchMails() throws Exception {
		searchMails();
	}
}
